﻿using System.Diagnostics;
using System.Windows.Input;
using MSDNWebcast.Commands;
using MSDNWebcast.Models;
using MSDNWebcast.Service;

namespace MSDNWebcast.ViewModels
{
    /// <summary>
    /// 数据加载页面
    /// </summary>
    public class LoadingViewModel : ViewModelBase
    {
        #region Fields

        /// <summary>
        /// 网络广播初始化
        /// </summary>
        private CourseInitializer m_Initializer;

        /// <summary>
        /// 下载Info.xml
        /// </summary>
        private ICommand m_StartLoadData;

        /// <summary>
        /// 更新Info.xml失败后，继续读取已有Info.xml
        /// </summary>
        private ICommand m_Continue;

        /// <summary>
        /// 加载是否完成
        /// </summary>
        private bool m_IsLoadingFinished;

        /// <summary>
        /// 
        /// </summary>
        private bool m_LoadingError;

        /// <summary>
        /// 下载进度
        /// </summary>
        private int m_DownloadProcess;

        /// <summary>
        /// 进度信息
        /// </summary>
        private string m_ProcessInfo;

        /// <summary>
        /// 设置代理
        /// </summary>
        private ICommand m_ProxySetting;

        /// <summary>
        /// 
        /// </summary>
        private bool? m_CanContinue;

        #endregion

        #region Constructs

        /// <summary>
        /// 
        /// </summary>
        public LoadingViewModel()
        {
            Retry.Execute(null);
        }

        #endregion

        #region Properties

        /// <summary>
        /// 加载数据是否结束
        /// </summary>
        public bool IsLoadingFinished
        {
            get
            {
                return m_IsLoadingFinished;
            }
            set
            {
                if (m_IsLoadingFinished != value)
                {
                    m_IsLoadingFinished = value;
                    OnPropertyChanged("IsLoadingFinished");
                }
            }
        }

        /// <summary>
        /// 处理进度
        /// </summary>
        public int DownloadProcess
        {
            get
            {
                return m_DownloadProcess;
            }
            set
            {
                if (m_DownloadProcess != value)
                {
                    m_DownloadProcess = value;
                    OnPropertyChanged("DownloadProcess");
                }
            }
        }

        /// <summary>
        /// 进度信息
        /// </summary>
        public string ProcessInfo
        {
            get
            {
                return m_ProcessInfo;
            }
            set
            {
                if (m_ProcessInfo != value)
                {
                    m_ProcessInfo = value;
                    OnPropertyChanged("ProcessInfo");
                }
            }
        }

        /// <summary>
        /// 处理进度中出错
        /// </summary>
        public bool LoadingError
        {
            get { return m_LoadingError; }
            set {
                if (m_LoadingError != value)
                {
                    m_LoadingError = value;
                    OnPropertyChanged("LoadingError");
                }
            }
        }

        /// <summary>
        /// 加载数据
        /// </summary>
        public ICommand Retry
        {
            get
            {
                if (m_StartLoadData == null)
                    m_StartLoadData = new DelegateCommand(StartDownload, () => true);
                return m_StartLoadData;
            }
        }

        /// <summary>
        /// 代理设置
        /// </summary>
        public ICommand ProxySetting
        {
            get
            {
                //调用IE设置代理
                return m_ProxySetting ??
                       (m_ProxySetting = new DelegateCommand(() => Process.Start("InetCpl.cpl"), () => true));
            }
        }

        /// <summary>
        /// 继续
        /// </summary>
        public ICommand Continue
        {
            get
            {
                return m_Continue ?? (m_Continue = new DelegateCommand(() =>
                                                                           {
                                                                               ServiceFactory.Instance.GetService
                                                                                   <CourseService>().LoadCourses();
                                                                               IsLoadingFinished = true;
                                                                           }, () => CanContinue));
            }
        }

        /// <summary>
        /// 是否能够继续
        /// </summary>
        public bool CanContinue
        {
            get
            {
                if (m_CanContinue == null)
                    m_CanContinue = CourseInitializer.ValidateInfoXml();
                return (bool)m_CanContinue;
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// 开始下载
        /// </summary>
        private void StartDownload()
        {
            LoadingError = false;
            DownloadProcess = 0;
            if (m_Initializer != null)
            {
                m_Initializer.PropertyChanged -= downloader_PropertyChanged;
            }
            m_Initializer = new CourseInitializer();
            m_Initializer.PropertyChanged += downloader_PropertyChanged;
            m_Initializer.ProcessAsync();
        }

        /// <summary>
        /// 处理状态发生变化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void downloader_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Process")
            {
                this.DownloadProcess = m_Initializer.Process;
            }
            if (e.PropertyName == "ProcessInfo")
            {
                this.ProcessInfo = m_Initializer.ProcessInfo;
            }
            if (e.PropertyName == "State" && m_Initializer.State == DownloadState.Finished)
            {
                IsLoadingFinished = true;
            }
            if (e.PropertyName == "ErrorException")
            {
                LoadingError = true;
            }
        }

        #endregion
    }
}
